package com.continuing.beef.service.bill.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.continuing.beef.domain.BillInfoLog;
import com.continuing.beef.service.BeefCommonService;
import com.continuing.beef.service.bill.IBillInfoService;
import com.continuing.beef.service.type.IBillTypeService;
import com.continuing.common.core.domain.model.LoginUser;
import com.continuing.common.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.continuing.beef.mapper.BillInfoMapper;
import com.continuing.beef.domain.BillInfo;
import org.springframework.transaction.annotation.Transactional;


/**
 * 账单信息Service业务层处理
 *
 * @author batata
 * @date 2021-09-26
 */
@Service
public class BillInfoServiceImpl implements IBillInfoService {

    @Autowired
    private BillInfoMapper billInfoMapper;


    @Autowired
    private BeefCommonService beefCommonService;

    @Autowired
    private BillInfoLogServiceImpl billInfoLogService;

    @Autowired
    private IBillTypeService billTypeService;


    /**
     * 查询账单信息
     *
     * @param id 账单信息主键
     * @return 账单信息
     */
    @Override
    public BillInfo selectBillInfoById(Long id) {
        return billInfoMapper.selectBillInfoById(id);
    }

    /**
     * 查询账单信息列表
     *
     * @param billInfo 账单信息
     * @return 账单信息
     */
    @Override
    public List<BillInfo> selectBillInfoList(BillInfo billInfo) {
        return billInfoMapper.selectBillInfoList(billInfo);
    }

    /**
     * 新增账单信息
     *
     * @param billInfo 账单信息
     * @return 结果
     */
    @Override
    public int insertBillInfo(BillInfo billInfo) {
        return billInfoMapper.insertBillInfo(billInfo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertBillInfo(BillInfo billInfo, LoginUser loginUser) {

        billInfo.setTimeCreate(new Date());

        // 创建者
        billInfo.setUserIdCreate(loginUser.getUserId());


        // 账单所属部门的id
        billInfo.setDeptIdBill(loginUser.getDeptId());

        // 账单所属公司的部门id
        billInfo.setDeptIdFirmBill(
                beefCommonService.getFirmDeptIdByDeptId(
                        loginUser.getDeptId()
                )
        );


        // 添加账单信息
        int i = insertBillInfo(billInfo);
        if (i != 1) {
            return i;
        }

        // 添加账单信息的log
        return insertBillInfoLogByBillInfo(billInfo);

    }


    /**
     * 修改账单信息
     *
     * @param billInfo 账单信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateBillInfo(BillInfo billInfo, LoginUser loginUser) {
        billInfo = packageBillInfoBeforeInsertOrUpdate(billInfo);

        billInfo.setUpdateTime(new Date());
        billInfo.setUserIdUpdate(loginUser.getUserId());

        // 更新
        billInfoMapper.updateBillInfo(billInfo);

        // 添加相应的日志
        return insertBillInfoLogByBillInfo(billInfo);
    }


    /**
     * 在插入或更新前封装实体类
     *
     * @param billInfo /
     * @return /
     */
    public BillInfo packageBillInfoBeforeInsertOrUpdate(BillInfo billInfo) {

        // 客户
        billInfo.setUserIdClient(billInfo.getClientUser());
        // 类型
        billInfo.setTypeId(billInfo.getBillTypeData());

        // 获得账单类型的说明
        billInfo.setTypeExplain(
                billTypeService.selectBillTypeByTypeId(
                        billInfo.getTypeId()
                ).getTypeExplain()
        );

        // 查询客户姓名
        billInfo.setUserNickNameClient(
                beefCommonService.getNickNameByUserId(
                        billInfo.getUserIdClient()
                )
        );

        if (isEquality(billInfo.getSum(), billInfo.getSumDiscount(), billInfo.getSumPayment())) {
            billInfo.setStatusBill(1);
        } else {
            billInfo.setStatusBill(0);
        }

        return billInfo;
    }

    /**
     * 根据账单的信息添加相应的log日志
     *
     * @param billInfo /
     * @return /
     */
    public int insertBillInfoLogByBillInfo(BillInfo billInfo) {
        BillInfoLog billInfoLog = new BillInfoLog();
        BeanUtils.copyBeanProp(billInfoLog, billInfo);
        billInfoLog.setIdBillInfo(billInfo.getId());
        billInfoLog.setId(null);
        return billInfoLogService.insertBillInfoLog(billInfoLog);
    }


    /**
     * 批量删除账单信息
     *
     * @param ids 需要删除的账单信息主键
     * @return 结果
     */
    @Override
    public int deleteBillInfoByIds(Long[] ids) {
        return billInfoMapper.deleteBillInfoByIds(ids);
    }

    /**
     * 删除账单信息信息
     *
     * @param id 账单信息主键
     * @return 结果
     */
    @Override
    public int deleteBillInfoById(Long id) {
        return billInfoMapper.deleteBillInfoById(id);
    }


    @Override
    public boolean isExceed(BigDecimal sum, BigDecimal sumDiscount, BigDecimal sumPayment) {
        // 优惠金额和付款金额的和
        BigDecimal sunAll = null;
        if (sumDiscount != null && sumPayment != null) {
            sunAll = sumDiscount.add(sumPayment);
        } else if (sumDiscount != null) {
            sunAll = sumDiscount;
        } else if (sumPayment != null) {
            sunAll = sumPayment;
        }
        if (sunAll == null) {
            return false;
        } else {
            return sunAll.compareTo(sum) > 0;
        }

    }


    @Override
    public boolean isEquality(BigDecimal sum, BigDecimal sumDiscount, BigDecimal sumPayment) {
        // 优惠金额和付款金额的和
        BigDecimal sunAll = null;
        if (sumDiscount != null && sumPayment != null) {
            sunAll = sumDiscount.add(sumPayment);
        } else if (sumDiscount != null) {
            sunAll = sumDiscount;
        } else if (sumPayment != null) {
            sunAll = sumPayment;
        }
        if (sunAll == null) {
            return false;
        } else {
            return sunAll.compareTo(sum) == 0;
        }
    }


}
